package com.xukang.batch.processor;

import com.xukang.batch.po.Users;
import com.xukang.batch.service.UserService;
import org.springframework.batch.item.ItemReader;

import java.util.Iterator;
import java.util.List;


/**
 * 使用@StepScope和@JobScope
 * 通过 Spring 的作用域确保每次启动获取新实例或参数
 */
public class MyItemReader implements ItemReader<Users> {

    private final Iterator<Users> dataIterator;

    public MyItemReader(UserService userService) {
        System.out.println("MyItemReader");
        List<Users> data = userService.findAll();
        this.dataIterator = data.iterator();
    }

    @Override
    public Users read() {
        System.out.println("read");
        if (dataIterator.hasNext()) {
            return dataIterator.next();
        }
        return null;
    }
}